package com.shoponline.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.shoponline.po.User;
import com.shoponline.service.UserService;

public class UserAction extends ActionSupport{

	private static final long serialVersionUID = 1L;
	
	private UserService userService;
	
	private String username;
	private String email;
	private String password;
	
	
	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public void checkUserName() throws IOException{
		
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out = response.getWriter();
		
		if(userService.checkUserName(username) == false){
			
			//用户名不唯一
			System.out.println("用户名不唯一");
			
			out.print("false");
			
			
		}else{
			
			//用户名唯一
			System.out.println("用户名唯一");
			out.print("true");
			
		}
		
	}
	
	public void checkEmail() throws IOException{
		
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out = response.getWriter();
		if(userService.checkEmail(email)==false){
			
			//电子邮箱不唯一
			out.print("false");
			
		}else{
			
			//电子邮箱唯一
			
			out.print("true");
			
		}
		
	}
	
	public void saveUser() throws IOException{
		
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out = response.getWriter();
		
		
		if(username.length()>0&&email.length()>0&&password.length()>0){
			
			System.out.println(username+","+email+","+password);
			if(userService.save(username, email, password)){
				
				out.print("true");
			}else{
				
				out.print("false");
			}
			
		}
		
		
	}
	
}
